package com.beself.controller;

import com.beself.service.ChallengeService;
import com.beself.until.RestJson;
import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @ProjectName: beself
 * @Package: com.beself.controller
 * @ClassName: ChallengeController
 * @Description: java类作用描述
 * @Author: 樊晋源
 * @CreateDate: 2019/10/18 19:32
 * @Version: 1.0
 */
@Controller
public class ChallengeController {
    @Resource
    private ChallengeService challengeService;
    @RequestMapping("/api/challenge")
    @ResponseBody
    public RestJson challengeClock(int uId,int routeId,int taskId,int ci) {
        System.out.println(ci);
        if (ci == 1) {
            //直接存时间
            int i = challengeService.insertone(uId, routeId, taskId, ci);
            if (i == 1) {
                return new RestJson(true, "成功创建", null);
            }
        } else {
            //先查询时间时间差
            int a = challengeService.selectlastdate(uId, routeId, taskId);
            //int b = 1;
            int b = 86400000;
            if (a > b) {
                int c = challengeService.updateone(uId, routeId, taskId, ci);
                if (c > 0) {
                    //代表第三次更新成功了
                    if (ci == 3) {
                        int d = challengeService.updatestate(uId, routeId, taskId);
                        System.out.println(d);
                    }
                }
                return new RestJson(true, "成功更新", null);
            } else {
                return new RestJson(false, "不足一天", null);
            }
        }
        return new RestJson(false,"失败",null);
    }
}
